package ink.xiaobaibai.controller.clientUser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ink.xiaobaibai.annotation.InsidePublicResources;
import ink.xiaobaibai.autoConfig.roleResources.ResourcesRoleMapping;
import ink.xiaobaibai.common.MyRole;
import ink.xiaobaibai.entity.ClientUser;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.IClientOrderService;
import ink.xiaobaibai.service.IClientUserService;
import ink.xiaobaibai.service.IMonthlyfeeOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

/**
 * @description: 客户接口
 * @author: 小白白
 * @create: 2021-06-05
 **/

@RestController
@RequestMapping("/clientUser")
@Api(tags = "小程序客户接口")
@CrossOrigin
@InsidePublicResources
public class ClientUserController {

    @Autowired
    private IClientUserService clientUserService;

    @Autowired
    private IClientOrderService onePayOrderService;

    @Autowired
    private IMonthlyfeeOrderService monthlyfeeOrderService;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    @ApiOperation("获取用户集合列表")
    @ResourcesRoleMapping(resourceName = "小程序客户列表", includeRoleNames = {MyRole.ADMIN, MyRole.POWER_CLIENT})
    public ResponseFormat getClientUser(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                        @RequestParam(value = "userId", required = false) Integer userId) {
        QueryWrapper<ClientUser> q1 = new QueryWrapper<>();
        q1.select("id", "user_nickname", "user_phone", "create_time", "user_image");
        if (userId != null) {
            q1.eq("id", userId);
            Page<ClientUser> page = new Page<>(1, 1, 1, false);
            page.setRecords(Collections.singletonList(this.clientUserService.getOne(q1)));
            return ResponseFormat.success(page);
        } else {
            Page<ClientUser> page = this.clientUserService.page(new Page<>(pageNum, pageSize), q1);
            return ResponseFormat.success(page);
        }
    }

    @GetMapping("/item/{id}")
    @ApiOperation("获取单个客户信息")
    public ResponseFormat getClientItem(@PathVariable("id") Integer id) {
        ClientUser clientUser = this.clientUserService.getById(id);
        return ResponseFormat.success(clientUser);
    }

//    /**
//     * 查看当前用户的单次订单
//     */
//    @GetMapping("/user/onePay/{userId}")
//    @ApiOperation("查看用户单次支付订单")
//    public ResponseFormat getOnePayListByUserId(@PathVariable("userId")Integer id,
//                                        @RequestParam(value = "pageNum",defaultValue = "1",required = false)Integer pageNum,
//                                        @RequestParam(value = "pageSize",defaultValue = "10",required = false)Integer pageSize) {
//        QueryWrapper<ClientOrder> q1=new QueryWrapper<>();
//        q1.eq("user_id",id);
//        Page<ClientOrder> page = onePayOrderService.page(new Page<>(pageNum, pageSize), q1);
//        return ResponseFormat.success(page);
//    }
//
//    /**
//     * 查看当前用户的包月订单
//     */
//    @GetMapping("/user/monthly/{userId}")
//    @ApiOperation("查看当前用户的包月订单")
//    public ResponseFormat getMonthlyListByUserId(@PathVariable("userId")Integer id,
//                                                 @RequestParam(value = "pageNum",defaultValue = "1",required = false)Integer pageNum,
//                                                 @RequestParam(value = "pageSize",defaultValue = "10",required = false)Integer pageSize){
//        QueryWrapper<MonthlyfeeOrder> q1=new QueryWrapper<>();
//        q1.eq("user_id",id);
//        Page<MonthlyfeeOrder> page = monthlyfeeOrderService.page(new Page<>(pageNum, pageSize), q1);
//        return ResponseFormat.success(page);
//    }

}
